import requests
import json
import redis
import time
import datetime

conn = redis.Redis()
order_id = '984062106857857'
proxys = conn.smembers('un_proxy')
start_time = 10 * 60 * 60 + 0 * 0 + 0
end_time = 22 * 60 * 60 + 60 * 30 + 0
now = datetime.datetime.now()
now_seconds = now.hour * 60 * 60 + now.minute * 60 + now.second
own_proxy = ['','http://42.51.205.96:16818',
 'http://121.42.148.121:16818',
 'http://106.12.214.83:16818',
 'http://122.114.234.157:16818',
 'http://211.149.189.148:16818',
 'http://106.58.213.7:16818',
 'http://106.12.214.212:16818',
 'http://114.215.174.49:16818',
 'http://106.12.23.200:16818',
 'http://121.42.177.10:16818',
 'http://121.41.11.179:16818',
 'http://112.74.206.133:16818',
 'http://43.226.164.60:16818',
 'http://122.114.69.239:16818',
 'http://121.199.1.230:16818',
 'http://42.51.206.156:16818',
 'http://42.51.206.58:16818',
 'http://122.114.197.131:16818',
 'http://36.103.242.231:16818',
 'http://116.255.191.105:16818',
 'http://116.255.161.203:16818']

is_own = True

proxy_cnt = len(own_proxy)

def add_du():
    for i in own_proxy:
        conn.sadd('un_proxy', i)

def add_proxy(proxy_cnt):
    add_du()
    length = conn.scard('un_proxy')
    if length < proxy_cnt:
        url = 'http://dps.kdlapi.com/api/getdps'
        data = {
            'orderid': '905560704797399',
            'num': 1,
            'ut': 3,
            'dedup': 0,
            'format': 'json'
        }
        rsp = requests.get(url, params=data)
        if rsp.status_code == 200:
            data = json.loads(rsp.content.decode())
            proxy_list = data['data']['proxy_list']
            for proxy in proxy_list:
                http_proxy = "http://" + proxy
                #try:
                #    rsp = requests.get("http://upay.10010.com/npfwap/npfMobWap/bankcharge/index.html?version=wap&desmobile=6672070486447365", proxies={'http': http_proxy}, timeout=10)
                #except Exception as e:
                #    print(e)
                #    continue
                conn.sadd("un_proxy", "http://" + proxy)

                print("un_proxy", http_proxy)

def add_wd_proxy(proxy_cnt):
    # add_du()
    length = conn.scard('un_proxy')
    if length < proxy_cnt:
        # url = 'http://h.wandouip.com/get/ip-list?pack=0&num=2&xy=1&type=1&lb=\n&mr=1&'
        url = 'http://api.wandoudl.com/api/ip?app_key=5bf5aef72ea5366cd4617013688c786e&pack=0&num=2&xy=1&type=1&lb=\n&mr=2&'
        rsp = requests.get(url)
        if rsp.status_code == 200:
            proxy_list = rsp.text.split()
            for proxy in proxy_list:
                http_proxy = "http://" + proxy
                #print(http_proxy)
                #try:
                #    rsp = requests.get("http://upay.10010.com/npfwap/npfMobWap/bankcharge/index.html?version=wap&desmobile=6672070486447365", proxies={'http': http_proxy}, timeout=10)
                #except Exception as e:
                #    print(e)
                #    continue
                conn.sadd("un_proxy", "http://" + proxy)

                print("un_proxy", http_proxy)

if __name__ == "__main__":
    while True:
        try:
            add_wd_proxy(proxy_cnt)
            # add_proxy(proxy_cnt)
        except Exception as e:
            print(e)
        if 2 * 60 * 60 + 30 * 60 + 0 < now_seconds < 5 * 60 * 60 + 30 * 60 + 0:
            proxy_cnt =25 
        elif 8 * 60 * 60 + 30 * 60 + 0 < now_seconds < 11 * 60 * 60 + 0 * 60 + 0:
            proxy_cnt = 70 
        elif 11 * 60 * 60 + 0 * 60 + 0 < now_seconds < 21 * 60 * 60 + 30 * 60 + 0:
            proxy_cnt = 70 
        elif 21 * 60 * 60 + 30 * 60 + 0 < now_seconds < 22 * 60 * 60 + 30 * 60 + 0:
            proxy_cnt = 60 
        elif 22 * 60 * 60 + 30 * 60 + 0 < now_seconds < 23 * 60 * 60 + 40 * 60 + 0:
            proxy_cnt = 60
        else:
            proxy_cnt = 40 

        time.sleep(2)
        now = datetime.datetime.now()
        now_seconds = now.hour * 60 * 60 + now.minute * 60 + now.second
